﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Xunit;
using System.IO;

namespace NMigrate.Tests
{
	public class ConfigFileHandlerTests
	{
		[Fact]
		public void CanHandleDefaultWebConfig()
		{
			using(var directoryTester = new DirectoryTester())
			{
				Migration migration = new Migration();
				var configHandler = migration.FileHandlers.Find<ConfigFileHandler>();
				configHandler.DefaultWebSitePath = directoryTester.Directory.FullName;
				var result = migration.MigrateFile("Data/1.Web.config");
				Assert.True(result.Success);
				directoryTester.AssertFileExist("Web.config");
			}
		}
		[Fact]
		public void CanHandleDefaultWebConfigWithoutVersionNumber()
		{
			using(var directoryTester = new DirectoryTester())
			{
				Migration migration = new Migration();
				var configHandler = migration.FileHandlers.Find<ConfigFileHandler>();
				configHandler.DefaultWebSitePath = directoryTester.Directory.FullName;
				var result = migration.MigrateFile("Data/Web.config");
				Assert.True(result.Success);
				directoryTester.AssertFileExist("Web.config");
			}
		}
		[Fact]
		public void CanHandleDefaultAppConfig()
		{
			using(var directoryTester = new DirectoryTester())
			{
				Migration migration = new Migration();
				var configHandler = migration.FileHandlers.Find<ConfigFileHandler>();
				configHandler.DefaultAppExePath = Path.Combine(directoryTester.Directory.FullName, "this.exe");
				var result = migration.MigrateFile("Data/App.config");
				Assert.True(result.Success);
				directoryTester.AssertFileExist("App.config");
			}
		}
		[Fact]
		public void CanHandleAppConfig()
		{
			using(var directoryTester = new DirectoryTester())
			{
				Migration migration = new Migration();
				var configHandler = migration.FileHandlers.Find<ConfigFileHandler>();
				configHandler.ApplicationExePaths.Add(directoryTester.MakeFilePath("MyApp.exe"));
				var result = migration.MigrateFile("Data/MyApp.exe.config");
				Assert.True(result.Success);
				directoryTester.AssertFileExist("MyApp.exe.config");
			}
		}
		[Fact]
		public void CanHandleWebConfig()
		{
			using(var directoryTester = new DirectoryTester("MySite"))
			{
				Migration migration = new Migration();
				var configHandler = migration.FileHandlers.Find<ConfigFileHandler>();
				configHandler.WebsitePaths.Add(directoryTester.Directory.FullName);
				var result = migration.MigrateFile("Data/MySite.config");
				Assert.True(result.Success);
				directoryTester.AssertFileExist("Web.config");
			}
		}
	}
}
